var mRequest = require('../goodsDetailsService.js');

let app = getApp()
/**
 * 赞赏果冻View
 */
Component({

  options: {
    // 在组件定义时的选项中启用多slot支持
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   * 用于组件自定义设置
   */
  properties: {
    clickNum: {
      type: String,
      value: '',
    },
    // 赞赏按钮是否显示
    isPraiseVisible: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 私有数据,组件的初始数据
   * 可用于模版渲染
   */
  data: {
    rewardData: {},
    goodsId: '',
    rewardIntegral: '',
    goodsDetailsData: {},
    currentPage: 0
  },

  methods: {

    initData: function (data, goodId) {
      this.setData({
        goodsDetailsData: data,
        goodsId: goodId
      })
      // 据商品ID获取商品的所有打赏
      this.showReward()
    },

    /**
     * 赞赏一个果冻点击事件
     */
    pratseClick: function (e) {
      // 获取表单id
      let formId = e.detail.formId;
      getApp().saveUserFormId(formId, 14);
      console.log("formId-14", formId)

      this.showPraiseCandy()
    },

    /**
     * 赞赏一个果冻接口
     */
    showPraiseCandy: function () {
      //  用户状态 0、自己，1、其他人
      if (this.data.goodsDetailsData.userStatus == 0){
          wx.showToast({
            title: '您不能赞赏自己',
            icon: 'none'
          })
          return
        }
      var that = this
      var param = {
        userId: app.globalData.userId,
        goodsId: this.data.goodsId,
        rewardIntegral: this.data.goodsDetailsData.rewardIntegral,
      }
      mRequest.praiseGoods(param,
        function (data) {
          console.log('赞赏一个果冻  成功')
          that.triggerEvent("refreshGoods")
          that.setData({
            currentPage: 0
          })
          that.showReward()
        });
    },

    /**
     * 据商品ID获取商品的所有打赏
     */
    showReward: function () {
      var that = this
      var param = {
        goodsId: this.data.goodsId,
        startPage: this.data.currentPage
      }
      mRequest.getRewardData(param,
        function (data) {
          console.log('据商品ID获取商品的所有打赏  成功')
          if (that.data.currentPage == 0) {
            that.setData({
              rewardData: data,
            })
          } else {
            let listData = data.tRewardListEntities
            if (listData && listData.length > 0) {
              let moreData = that.data.rewardData.tRewardListEntities.concat(listData)
              that.data.rewardData.tRewardListEntities = moreData
              that.setData({
                rewardData: that.data.rewardData,
              })
            } else {
              that.setData({
                currentPage: that.data.currentPage - 1,
              })
            }
          }

        });
    },

    /**
     * 打赏的人加载更多
     */
    moreLoadListener: function () {
      console.log('加载更多打赏的人');
      this.setData({
        currentPage: this.data.currentPage + 1
      })
      this.showReward()
    }

  }
})
